package com.loong.coupon.controller.backend;

import com.loong.common.enumerate.CouponStatus;
import com.loong.common.mvc.BaseBackendController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.coupon.common.backend.param.CouponAndApplyRuleParam;
import com.loong.coupon.common.backend.param.CouponQueryParam;
import com.loong.coupon.handler.CouponHandler;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 处理后台优惠券
 * @Author: LiuQun
 * @Date: 2021/1/7 16:32
 */
@RestController
@RequestMapping("manage/order/coupon")
public class CouponController extends BaseBackendController {

    @Resource
    private CouponHandler couponHandler;

    /**
     * 新增优惠券和优惠券适用规则
     * @param param 优惠券及适用规则参数
     * @return 响应的结果
     */
    @PostMapping("save")
    public Result saveCouponAndApplyRule(@Validated({CommonInsert.class})CouponAndApplyRuleParam param){
        return handlerExecutor.execute(() -> couponHandler.saveCouponAndApplyRule(param));
    }

    /**
     * 查询优惠券详情
     * @param param 优惠券id
     * @return 响应的结果
     */
    @PostMapping("find/couponDetail")
    public Result findCouponDetail(@Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> couponHandler.findCouponDetail(param.getId()));
    }

    /**
     * 更新优惠券及优惠券适用规则
     * @param param 优惠券参数
     * @return 响应的结果
     */
    @PostMapping("update")
    public Result updateCouponAndApplyRule(@Validated({CommonUpdate.class}) CouponAndApplyRuleParam param){
        return handlerExecutor.execute(() -> couponHandler.updateCouponAndApplyRule(param));
    }

    /**
     * 审核通过优惠券
     * @param param 优惠券id
     * @return 响应的结果
     */
    @PostMapping("approve")
    public Result approveCoupon(@Validated({CommonValidate.class}) BaseIdParam param){
        return handlerExecutor.execute(() -> couponHandler.updateCouponStatus(param.getId(), CouponStatus.APPROVED.getCode()));
    }

    /**
     * 审核拒绝通过优惠券
     * @param param 优惠券id
     * @return 响应的结果
     */
    @PostMapping("reject")
    public Result rejectCoupon(@Validated({CommonValidate.class}) BaseIdParam param){
        return handlerExecutor.execute(() -> couponHandler.updateCouponStatus(param.getId(),CouponStatus.UNAPPROVED.getCode()));
    }

    /**
     * 使优惠券无效
     * @param param 优惠券id
     * @return 响应的结果
     */
    @PostMapping("invalid")
    public Result invalidCoupon(@Validated({CommonValidate.class}) BaseIdParam param){
        return handlerExecutor.execute(() -> couponHandler.updateCouponStatus(param.getId(),CouponStatus.INVALID.getCode()));
    }

    /**
     * 分页查询优惠券
     * @param param 优惠券查询参数
     * @return 响应的结果
     */
    @PostMapping("pageQuery/couponList")
    public Result pageQueryCoupon(CouponQueryParam param){
        return handlerExecutor.execute(() -> couponHandler.pageQueryCoupon(param));
    }

    /**
     * 批量删除优惠券
     * @param idList 优惠券id集合
     * @return 响应的结果
     */
    @PostMapping("batchDelete")
    public Result batchDeleteCoupon(@RequestParam("idList")List<Long> idList){
        return handlerExecutor.execute(() -> couponHandler.batchDeleteCoupon(idList));
    }

    /**
     * 获取优惠券类型
     * @return 响应的结果
     */
    @PostMapping("couponTypeList")
    public Result getCouponTypeList(){
        return handlerExecutor.execute(() -> couponHandler.getCouponType());
    }

    /**
     * 获取优惠券来源
     * @return 响应的结果
     */
    @PostMapping("couponSourceList")
    public Result getCouponSourceList(){
        return handlerExecutor.execute(() -> couponHandler.getCouponSource());
    }

}
